package top.someapp.dxf2dsvg.jfx;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import top.someapp.dxf2dsvg.base.ExportOptions;
import top.someapp.dxf2dsvg.kits.Numbers;

import javax.annotation.Nonnull;
import java.awt.*;

/**
 * @author zwz
 * Created on 2020-04-26
 */
public class LayerOptions implements JfxController {
    @FXML
    CheckBox chkExportLayer0;
    @FXML
    RadioButton rbAllLayersInOne;
    @FXML
    RadioButton rbOneLayerOneFile;
    @FXML
    RadioButton rbUseInclude;
    @FXML
    RadioButton rbUseExclude;
    @FXML
    TextArea taIncludes;
    @FXML
    TextArea taExcludes;
    @FXML
    ComboBox<String> cbxSize;
    private final ExportOptions options;

    public LayerOptions() {
        options = ExportOptions.getInstance();
    }

    @Override
    public void onCreate(@Nonnull JfxContext jfxContext) {
        ObservableList<String> items = cbxSize.getItems();
        /// TODO: 2020/4/27 read from resource file will be better!
        items.add("auto");
        items.add("1920x1080");
        items.add("1280x800");
        items.add("1024x768");
        items.add("800x600");

        chkExportLayer0.setSelected(options.isExportLayer0());

        rbAllLayersInOne.setSelected(ExportOptions.ALL_LAYERS_IN_ONE.equals(options.getLayerToSvg()));
        rbOneLayerOneFile.setSelected(ExportOptions.ONE_LAYER_ONE_FILE.equals(options.getLayerToSvg()));

        rbUseInclude.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                taIncludes.setDisable(!newValue);
                taExcludes.setDisable(newValue);
            }
        });
        rbUseInclude.setSelected(options.isUseInclude());
        rbUseExclude.setSelected(!options.isUseInclude());

        taIncludes.setText(options.getIncludes("\n"));
        taExcludes.setText(options.getExcludes("\n"));

        Dimension size = options.getSize();
        if (size == null) {
            cbxSize.getSelectionModel().select(0);
        } else {
            switch (size.width) {
                case 1920:
                    cbxSize.getSelectionModel().select(1);
                    break;
                case 1280:
                    cbxSize.getSelectionModel().select(2);
                    break;
                case 1024:
                    cbxSize.getSelectionModel().select(3);
                    break;
                case 800:
                    cbxSize.getSelectionModel().select(4);
                    break;
            }
        }
    }

    public void updateOptions() {
        options.setExportLayer0(chkExportLayer0.isSelected());
        if (rbAllLayersInOne.isSelected()) {
            options.setLayerToSvg(ExportOptions.ALL_LAYERS_IN_ONE);
        } else {
            options.setLayerToSvg(ExportOptions.ONE_LAYER_ONE_FILE);
        }
        options.setUseInclude(rbUseInclude.isSelected());
        options.setIncludes(taIncludes.getText().trim().split("\n"));
        options.setExcludes(taExcludes.getText().trim().split("\n"));
        String size = cbxSize.getSelectionModel().getSelectedItem();
        if ("auto".equals(size)) {
            options.setSize(null);
        } else {
            int[] values = Numbers.intValues(size, 'x');
            options.setSize(new Dimension(values[0], values[1]));
        }
    }
}
